package pl.poznan.put.kck;


import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

public class BinaryReader {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable(){
			public void run(){
				CalibrationFrame frame = new CalibrationFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}

}


class CalibrationFrame extends  JFrame{
	

	public CalibrationFrame(){
		setSize(800, 700);
		setLocationByPlatform(true);
		setTitle("HTD files analyzer");
		CalibrationPanel panel = new CalibrationPanel();
		add(panel);
	}
	private static final long serialVersionUID = 9171516427262113901L;
}

class CalibrationPanel extends JPanel implements ActionListener {


	
	public CalibrationPanel(){
		final JFileChooser fc = new JFileChooser();
		File dir = new File(".");
			fc.setCurrentDirectory(dir);
	        int returnVal = fc.showOpenDialog(CalibrationPanel.this);
	        
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fc.getSelectedFile();
	            //This is where a real application would open the file.
	            //
	            plik = new HTDLoader(file.getName());
	      	  plik.findMinMax();
	            //log.append("Opening: " + file.getName() + "." + newline);
	        } else {
	            System.exit(1);        }
	  
	  setLayout(new FlowLayout(FlowLayout.CENTER));
	}
	
	public void actionPerformed(ActionEvent event){
		
	}
	

	public void paintComponent(Graphics g){
	 Graphics2D g2 = (Graphics2D) g;
	 g2.setPaint(new Color(1,1,1)); 
	 g2.drawString("Opened file: ", 10, 20);
	 g2.drawBytes(plik.fName, 0, plik.fNameLength, 90, 20); 
	 g2.drawString("Created on: ", 10, 40);
	 g2.drawBytes(plik.dateString, 0, plik.dateLength, 90, 40); 
	 g2.drawString("Pressure scale:", 10, 60);
	 g2.drawString("MAX", 25, 90);
	 g2.drawString("MIN", 25, 592);
	 for(int i=plik.colorTable.length-1; i >= 0; i-=2) {
		 g2.setPaint(new Color(plik.colorTable[i][0], plik.colorTable[i][1], plik.colorTable[i][2]));
		 g2.drawLine(10, 80+(i/2), 20, 80+(i/2));
	 }
	 for(int i =0 ; i < plik.data.length; i++) {
		 int colIndex = plik.getColIndex(plik.data[i].pkPressure);
		 if(colIndex < 1024) g2.setPaint(new Color(255-plik.colorTable[colIndex][0], 255-plik.colorTable[colIndex][1], 255-plik.colorTable[colIndex][2]));
		 g2.drawLine(plik.scaleY(plik.data[i].pkY), plik.scaleX(plik.data[i].pkX), plik.scaleY(plik.data[i].pkY), plik.scaleX(plik.data[i].pkX));
	 }
	 
	}

	private static final long serialVersionUID = -3492820978557303480L;
	public int WIDTH = 700;
	public int HEIGHT = 700;
	public HTDLoader plik;
}